package cn.cxj.user.controller;

import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LogAnnotation;
import cn.cxj.basic.util.PageList;
import cn.cxj.user.domain.Logininfo;
import cn.cxj.user.query.LogininfoQuery;
import cn.cxj.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登陆信息接口
 * @author 陈鑫杰
 * @date 2022年1月23日21:47:24
 */
@RestController
@RequestMapping("/logininfo")
@Api(tags = "登陆信息接口",description = "登陆信息接口详细描述")
public class LogininfoController {

    @Autowired
    private ILogininfoService loginInfoService;

    /**
     * 新增或者修改 POST
     * @param loginInfo 登陆信息数据
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "登陆信息添加或修改",notes = "如果有id就是修改否则就是添加")
    @LogAnnotation(operateType = "登陆信息添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Logininfo loginInfo){
        try {
            if (loginInfo.getId() == null) {
                loginInfoService.add(loginInfo);
            } else {
                loginInfoService.update(loginInfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 删除 DELETE
     * @param id 登陆信息id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "登陆信息删除",notes = "删除时需要携带的id")
    @LogAnnotation(operateType = "登陆信息删除")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            loginInfoService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id查询 GET
     * @param id 登陆信息id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "登陆信息查询",notes = "查询时需要携带的id")
    @LogAnnotation(operateType = "登陆信息查询")
    public Logininfo findById(@PathVariable("id") Long id){
        return loginInfoService.queryById(id);
    }

    /**
     * 获取所有 GET
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询全部登陆信息")
    @LogAnnotation(operateType = "查询全部登陆信息")
    public List<Logininfo> findAll(){
        return loginInfoService.queryAll();
    }


    /**
     * 分页和高级查询 POST
     * @param query 查询参数
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询和高级查询",notes = "查询时需要携带的参数")
    @LogAnnotation(operateType = "分页查询和高级查询")
    public PageList<Logininfo> pageQuery(@RequestBody LogininfoQuery query){
        return loginInfoService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids 登陆信息id数组
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "批量删除时需要携带的id集合")
    @LogAnnotation(operateType = "批量删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            loginInfoService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }
}
